//
//  main.m
//  省市区
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    
    //    NSString *filepath = @"/Users/dllo/Desktop/DLS151034/oc课堂笔记/oc练习题/oc-6字典练习/省市区/省市区/File";//
    //    NSString *str = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"%@", str);
    
    NSString *filepath = @"/Users/dllo/Desktop/省市区/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];//将文件存到str 中
    //    NSLog(@"%@", str);
    NSArray *array = [str componentsSeparatedByString:@"\n"];//将文件中的元素存到数组中
    NSMutableArray *mArray = [NSMutableArray array];//省数组
    //    for (NSString *str in arr) {
    //        NSLog(@"%@", str);
    //    }
    //  取省
    
    for (NSInteger i = 0; i < array.count; i++) {
        NSString *s1 = [array objectAtIndex:i];
        //      省份
        if (![s1 hasPrefix:@" "]) {
            
            NSMutableArray *cityList = [NSMutableArray array];//建一个可变的数组来存每个省下面的市
            
            NSDictionary *pDic = [NSDictionary dictionaryWithObjectsAndKeys:s1, @"name", cityList, @"cityList", nil];//省的字典
            
            [mArray addObject:pDic];//将省字典添加到
            
            continue;//结束本次循环重新循环
            
        }
        
        //找城市
        
        if ([s1 hasPrefix:@" "] && ![s1 hasPrefix:@"   "]) {
            
            NSDictionary *pDic = [mArray lastObject];
            
            NSMutableArray *cityList = [pDic objectForKey:@"cityList"];  //指向
            
            //区列表
            
            NSMutableArray *districtList = [NSMutableArray array];
            
            NSDictionary *cDic = [NSDictionary dictionaryWithObjectsAndKeys:s1, @"name", districtList, @"districtList", nil];
            
            [cityList addObject:cDic];
            continue;
            
        }
        
        //找到区
        
        if ([s1 hasPrefix:@"    "]) {
            
            NSDictionary *pDic = [mArray lastObject];
            
            NSMutableArray *cityList = [pDic objectForKey:@"cityList"];
            
            NSDictionary *cDic = [cityList lastObject];
            
            NSMutableArray *districtList = [cDic objectForKey:@"districtList"];
            
            [districtList addObject:s1];
            
        }
        
    }
    
    
    NSLog(@"%@", mArray);
    
    for (int i = 0; i < [mArray count]; i++)
        
    {
        
        // NSLog(@"%@", [[mArray objectAtIndex:i] objectForKey:@"name"]);
        
        NSDictionary *pDic = [mArray objectAtIndex:i];
        
        NSLog(@"province : %@", [pDic objectForKey:@"name"]);
        
        NSMutableArray *cityList = [pDic objectForKey:@"cityList"];
        
        for (int j = 0; j < [cityList count]; j++)
            
        {
            
            // NSLog(@"%@", [cityList objectAtIndex:j]);
            
            NSDictionary *cDic = [cityList objectAtIndex:j];
            
            NSLog(@"cityList : %@", [cDic objectForKey:@"name"]);
            
            NSMutableArray *districtList = [cDic objectForKey:@"districtList"];
            
            for (int k = 0; k < [districtList count]; k++) {
                
                NSLog(@"districtList : %@", [districtList objectAtIndex:k]);
                
            }
            
        }
        
    }
    
    return 0;
}